
package com.wwy.monitor.controller;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.common.ServiceResponse;
import com.wwy.monitor.model.enumeration.DeleteEnum;
import com.wwy.monitor.model.tddl.dto.MonitorConfigIpRelDTO;
import com.wwy.monitor.model.tddl.query.MonitorConfigIpRelQuery;
import com.wwy.monitor.model.vo.ConfigIpRelBatchVO;
import com.wwy.monitor.service.MonitorConfigIpRelService;
import com.wwy.monitor.util.UserUtils;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/monitorConfigIpRel")
public class MonitorConfigIpRelController {
    @Resource
    private MonitorConfigIpRelService monitorConfigIpRelService;

    @PostMapping("/addBatch")
    @ResponseBody
    private ServiceResponse addBatch(HttpServletRequest request, @RequestBody ConfigIpRelBatchVO vo)
            throws IOException, ServletException {
        //参数校验
        boolean checkFlag = (vo != null && vo.getDetailId() != null && vo.getIpIds() != null
                && vo.getIpIds().size() >= 0);
        if (!checkFlag) {
            return ServiceResponse.paramIllegal("参数不全");
        }
        String empId = UserUtils.getBucEmpId(request);
        List<MonitorConfigIpRelDTO> data = Lists.newArrayList();
        for (Long ipId : vo.getIpIds()) {
            MonitorConfigIpRelDTO dto = new MonitorConfigIpRelDTO();
            dto.setDetailId(vo.getDetailId());
            dto.setIpId(ipId);
            dto.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
            dto.setCreateUser(empId);
            dto.setUpdateUser(empId);
            dto.setCreateTime(new Date());
            dto.setUpdateTime(new Date());
            data.add(dto);
        }
        return ServiceResponse.success(monitorConfigIpRelService.batchInsert(data));
    }

    @PostMapping("{id}/deleteById")
    private ServiceResponse deleteById(@PathVariable Long id) {
        if (null == id || id <= 0) {
            throw new MessageException("非法id");
        }
        return ServiceResponse.success(monitorConfigIpRelService.deleteById(id));
    }

    @PostMapping("{detailId}/deleteByDetailId")
    private ServiceResponse deleteByDetailId(@PathVariable Long detailId) {
        if (null == detailId || detailId <= 0) {
            throw new MessageException("非法id");
        }
        return ServiceResponse.success(monitorConfigIpRelService.deleteByDetailId(detailId));
    }

    @GetMapping("/getList")
    private ServiceResponse getList(MonitorConfigIpRelQuery monitorConfigIpRelQuery) {
        return ServiceResponse.success(monitorConfigIpRelService.selectByQuery(monitorConfigIpRelQuery));
    }
}